﻿using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace Nirvana
{
    public sealed class BuildInShaderCheckWindow : EditorWindow
    {
        private BuildInShaderImportRule buildInShaderImportRule;

        private Editor[] editors;
        private bool[] issucs;
        private Vector2 AssetRulesRect;
        private Vector2 starpos;
        private int index = -1;
        static int MinWidth = 475;

        [MenuItem("Nirvana/BuildIn Shader Check Rule", false)]
        public static void Init()
        {
            BuildInShaderCheckWindow window = (BuildInShaderCheckWindow)EditorWindow.GetWindow(typeof(BuildInShaderCheckWindow));
            window.Show();
            window.minSize = new Vector2(MinWidth, 475);
        }


        private void OnEnable()
        {
            this.hideFlags = HideFlags.HideAndDontSave;
            editors = new Editor[1];
            issucs = new bool[1];

            var staderAssets = AssetDatabase.FindAssets("t:BuildInShaderImportRule");
            if (staderAssets != null && staderAssets.Length > 0)
            {
                string guid = staderAssets[0];
                string path = AssetDatabase.GUIDToAssetPath(guid);
                this.buildInShaderImportRule = AssetDatabase.LoadAssetAtPath<BuildInShaderImportRule>(path);
                this.editors[editors.Length - 1] = Editor.CreateEditor(this.buildInShaderImportRule);
            }
        }

        private void OnFocus()
        {
            this.Repaint();
        }
        private void OnSelectionChange()
        {
            this.Repaint();
        }
        private void OnGUI()
        {
            GUILayout.Space(10f);
            EditorGUILayout.LabelField("BuildIn Shader Checker", GUITheme.LabelTitleStyle);
            GUILayout.Space(10f);
            GUILayout.Label("BuildIn Shader Rules:", GUITheme.TitleStyle);
            this.AssetRulesRect = EditorGUILayout.BeginScrollView(this.AssetRulesRect, GUITheme.ContainerStyle);
            DrawAssetRulesUI();
            EditorGUILayout.EndScrollView();

            EditorGUILayout.Space();
            EditorGUILayout.BeginHorizontal(GUITheme.ListItemHeaderStyle);
            if (GUILayoutEx.ActionButton("Check Assets"))
            {
                if (this.buildInShaderImportRule != null)
                {
                    this.buildInShaderImportRule.FixMaterialsStandardShader(false);
                }
            }
            if (GUILayoutEx.ActionButton("查找替换了内置Shader的Material"))
            {
                this.buildInShaderImportRule.FindMaterialsStandardShader();
            }
            EditorGUILayout.EndHorizontal();

            List<AssetCheckReportItem> list = new List<AssetCheckReportItem>();
            if (this.buildInShaderImportRule != null)
            {
                list.AddRange(buildInShaderImportRule.Report.Reports);
            }
            EditorGUILayout.LabelField("Reports:(鼠标滚轮滑动)");
            AssetCheckReportItem.Show(list, ref this.starpos, ref this.index);
        }

        private void DrawAssetRulesUI()
        {
            if (this.buildInShaderImportRule == null)
            {
                EditorGUILayout.LabelField("There is no BuildIn Shader Import Rule  in the project.");
                return;
            }
            if (this.buildInShaderImportRule != null)
            {
                Editor editor = this.editors[this.editors.Length - 1];
                this.issucs[this.issucs.Length - 1] = GUILayoutEx.Title(string.Format("{0} ({1})", buildInShaderImportRule.GetDirectorName(), buildInShaderImportRule.name), this.issucs[this.issucs.Length - 1]);
                if (this.issucs[this.issucs.Length - 1])
                {
                    GUILayoutEx.BeginContents();
                    editor.DrawDefaultInspector();
                    GUILayoutEx.EndContents();
                }
            }
        }





    }
}

